package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

  @Autowired
  private SupplierMapper supplierMapper;

  /**
   * 物资来源列表
   * @param pageNum
   * @param pageSize
   * @param supplierVO
   * @return
   */
  @Override
  public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
    PageHelper.startPage(pageNum,pageSize);
    Example s = new Example(Supplier.class);
    Example.Criteria criteria = s.createCriteria();
    s.setOrderByClause("sort asc");
    if (supplierVO.getName()!=null&&!"".equals(supplierVO.getName())){
      criteria.andLike("name","%" + supplierVO.getName() + "%");
    }
    if (supplierVO.getContact()!=null&&!"".equals(supplierVO.getContact())){
      criteria.andLike("contact","%" + supplierVO.getContact() + "%");
    }
    if (supplierVO.getAddress()!=null&&!"".equals(supplierVO.getAddress())){
      criteria.andLike("address","%" + supplierVO.getAddress() + "%");
    }
    List<Supplier> suppliers = supplierMapper.selectByExample(s);
    List<SupplierVO> supplierVOS = SupplierConverter.converterToVOlist(suppliers);
    PageInfo<Supplier> info = new PageInfo<>(suppliers);
    return new PageVO<>(info.getTotal(),supplierVOS);
  }

  /**
   * 添加物资来源信息
   * @param supplierVO
   * @return
   */
  @Override
  public Supplier add(SupplierVO supplierVO) {
    Supplier supplier = new Supplier();
    BeanUtils.copyProperties(supplierVO,supplier);
    supplier.setCreateTime(new Date());
    supplier.setModifiedTime(new Date());
    supplierMapper.insert(supplier);
    return supplier;
  }

  /**
   * 编辑物资来源信息页面
   * @param id
   * @return
   * @throws BusinessException
   */
  @Override
  public SupplierVO edit(Long id) throws BusinessException {
    Supplier supplier = supplierMapper.selectByPrimaryKey(id);
    if (supplier==null){
      throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资来源信息不存在");
    }
    return SupplierConverter.converterToSupplierVO(supplier);
  }

  /**
   * 修改物资来源信息
   * @param id
   * @param supplierVO
   * @return
   */
  @Override
  public Supplier update(Long id, SupplierVO supplierVO) {
    Supplier supplier = new Supplier();
    BeanUtils.copyProperties(supplierVO,supplier);
    supplier.setModifiedTime(new Date());
    supplierMapper.updateByPrimaryKey(supplier);
    return supplier;
  }

  @Override
  public void delete(Long id) {
    supplierMapper.deleteByPrimaryKey(id);
  }
}
